#! /bin/bash
# 注：使用此脚本需要 1.修改主机名解析	2.自定义ansible运行目录及主机清单

# 1.部署epel源
if [ ! -f "/etc/yum.repos.d/epel.repo" ];then
	wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
fi
	
# 2.安装ansible
yum -y install ansible
	
# 3.配置主机名解析
cat >> /etc/hosts <<-EOF
192.168.248.133 vm2
192.168.248.134 vm3
192.168.248.135 vm4
192.168.248.136 vm5
192.168.248.137 vm6
192.168.248.138 vm7
EOF

#  4.配置ssh密钥验证
ssh-keygen -f /root/.ssh/id_rsa -N ''
for i in vm2 vm3 vm4 vm5 vm6 vm7 
do
ssh-copy-id root@$i
done

# 5.初始化ansible至家目录
mkdir -p /root/ansible/{roles,playbooks}				# 角色目录、剧本目录
cat >> /root/ansible/ansible.cfg <<-EOF					# 主配置文件
[defaults]
inventory = /root/ansible/hosts
roles_path = /root/ansible/roles
EOF

cat >> /root/ansible/hosts	<<-EOF						# 主机清单文件
[webserver]
vm4
vm5
EOF